﻿using System;
using System.ServiceProcess;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;

namespace TeamDeploy.Tasks
{
    class StartService : TaskBase
        {
            public override bool Execute()
            {
                try
                {
                    ServiceController controller = new ServiceController(ServiceName, TargetMachine);
                    controller.Start();

                    if (BuildEngine != null)
                    {
                        Log.LogMessage("Start Service: " + ServiceName + " on " + TargetMachine +
                                       " completed successfully.");
                    }
                    return true;
                }
                catch (Exception ex)
                {
                    if (BuildEngine != null)
                    {
                        Log.LogError("Error starting service: " + ServiceName + " TargetMachine=" + TargetMachine +
                                     ":::" + ex);
                    }
                    return false;
                }
            }

            [Required]
            public string ServiceName { get; set; }

            [Required]
            public string TargetMachine { get; set; }
        }
}

